package com.asen.commons.core.jdbc.handler;

import com.asen.commons.core.base.model.BaseEnumEntry;
import com.asen.commons.core.base.ObjectEnum;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.sql.*;
import java.util.Collection;

/**
 * StringEnum
 *
 * @author Gary
 * @version 1.0
 * @date 2018-11-29
 */
public class StringTypeHandler extends BaseTypeHandler<String> {

    @Override
    public int getSqlType() {
        return Types.VARCHAR;
    }

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, String parameter)
            throws SQLException {
        ps.setString(i, parameter);
    }

    @Override
    public String getNullableResult(ResultSet rs, String columnName)
            throws SQLException {
        return rs.getString(columnName);
    }

    @Override
    public String getNullableResult(ResultSet rs, int columnIndex)
            throws SQLException {
        return rs.getString(columnIndex);
    }

    @Override
    public String getNullableResult(CallableStatement cs, int columnIndex)
            throws SQLException {
        return cs.getString(columnIndex);
    }

    @Override
    public String toSqlValue(Object value) {
        if (value == null) {
            return null;
        }

        if (value instanceof String) {
            return "\'" + value + "\'";
        }

        if (value instanceof String[]) {
            return getSqlValue((String[]) value, true);
        }

        if (value instanceof Collection) {
            Collection collection = (Collection) value;
            return getSqlValue((String[]) collection.toArray(new String[collection.size()]), true);
        }

        if (value instanceof ObjectEnum) {
            return "\'" + ((ObjectEnum) value).getValue() + "\'";
        }

        return "\'" + value + "\'";
    }

    @Override
    public String getValue(Object value) throws Exception {
        if (value == null) {
            return null;
        }

        if (String.class.isAssignableFrom(value.getClass())) {
            return (String) value;
        }

        if (Date.class.isAssignableFrom(value.getClass())) {
            String sDate = DateFormatUtils.format((Date) value, "yyyy-MM-dd HH:mm:ss");
            if (sDate.endsWith(" 00:00:00")) {
                sDate = sDate.substring(0, sDate.length() - " 00:00:00".length());
            }
            return sDate;
        }

        if (ObjectEnum.class.isAssignableFrom(value.getClass())) {
            ObjectEnum actualValue = (ObjectEnum) value;
            return String.valueOf(actualValue.getValue().toString());
        }

        if (BaseEnumEntry.class.isAssignableFrom(value.getClass())) {
            BaseEnumEntry actualValue = (BaseEnumEntry) value;
            return actualValue.getValue();
        }

        //if (DictItem.class.isAssignableFrom(value.getClass())) {
        //    DictItem item = (DictItem) value;
        //    return item.getValue();
        //}

        return String.valueOf(value);
    }
}
